import React from 'react'
import { Modal } from 'antd'
import moment from 'moment'
import style from './Modal.module.less'
import { download } from '@js/downloadRename'

// 格式化时间
function formatDate (t, format = 'YYYY-MM-DD') {
  if (!t) {
    return t
  }
  return moment(t * 1000).format(format)
}

function renderEMSPhones (d) {
  if (d.to_type === '公告送达') {
    return <span />
  }
  if (d.to_type !== 'EMS') {
    return <div className={style['text-cell']}>送达人员：{d.sender || '无'}</div>
  }
  return (
    <>
      <div className={style['text-cell']}>移动电话：{d.mobile || '无'}</div>
      <div className={style['text-cell']}>住宅电话：{d.tel || '无'}</div>
      <div className={style['text-cell']}>办公电话：{d.tel_office || '无'}</div>
      <div className={style['text-cell']}>收件人：{d.contract || '无'}</div>
      <div className={style['text-cell']}>单位：{d.structure || '无'}</div>
    </>
  )
}

function renderNotary (d) {
  if (d.to_type !== '公证送达') {
    return null
  }
  return (
    <div className={style['text-cell']}>公证人：{d.notary || '无'}</div>
  )
}

function renderCaseProcessor (d) {
  if (d.to_type !== 'EMS') {
    return null
  }
  return (
    <>
      <div className={style['text-cell']}>办案秘书：{d.processer || '无'}</div>
      <div className={style['text-cell']}>电话：{d.processer_tel || '无'}</div>
    </>
  )
}

function renderDatePicker (d) {
  const { to_type: type } = d
  if (type === '公告送达') {
    return (
      <div>
        <div className={style['text-cell']}>刊登日期：{formatDate(d.send_datetime) || '无'}</div>
      </div>
    )
  }
  let name = 'send_datetime'
  let label = '收寄日期'
  if (type !== 'EMS') {
    name = 'arrive_datetime'
  }
  if (type === '公证送达' || type === '现场送达') {
    label = '送达日期'
  } else if (type === '直接送达') {
    label = '签收日期'
  }
  return <div className={style['text-cell']}>{label}：{formatDate(d[name]) || '无'}</div>
}

function renderFileList (d) {
  const list = (d.file || [])
  return (
    <div className={style['text-cell']}>
      <span>送达情况材料：</span>
      {list.length ? (
        list.map((li, i) => {
          return <span key={i} className={style.file}>{i + 1}、<a onClick={() => download(li.path, li.original_filename)}>{li.original_filename}</a></span>
        })
      ) : '无'}
    </div>
  )
}

function Main ({ visible, hide, data: d }) {
  if (!d) {
    return null
  }
  const { to_type: type, status } = d || {}
  const isNotice = type === '公告送达'
  return (
    <Modal
      open={visible}
      title='查看详情'
      width={600}
      onCancel={hide}
      footer={null}
      maskClosable={false}
      destroyOnClose
    >
      <div className={style['text-cell']}>送达类型：{type || '无'}</div>
      {!isNotice && <div className={style['text-cell']}>是否为最后一个为人所知的地址：{d.islast_known_place === 1 ? '是' : '否'}</div>}
      <div className={style['text-cell']}>送达情况：{status || '无'}</div>
      <div className={style['text-cell']}>仲裁地位：{d.action_subject || '无'}</div>
      {!isNotice && <div className={style['text-cell']}>地址类型：{d.type || '无'}</div>}
      {!isNotice && <div className={style['text-cell']}>地址：{d.address || '无'}</div>}
      {isNotice && <div className={style['text-cell']}>公告媒体：{d.publication_media || '无'}</div>}
      {renderEMSPhones(d)}
      {renderNotary(d)}
      {renderCaseProcessor(d)}
      {renderDatePicker(d)}
      {renderFileList(d)}
      <div className={style['text-cell']}>送达内容：{d.content || '无'}</div>
      <div className={style['text-cell']}>备注：{d.remark || '无'}</div>
    </Modal>
  )
}

export default Main
